import axios from "axios"
const baseURL = "http://127.0.0.1:8888/api/private/v1/"
axios.defaults.baseURL = baseURL
const state = {
    name: '',
    token: ''
}
const getters={
    username:state=>localStorage.getItem('username')
}
const mutations = {
    getUsers(state, payload) {
        // console.log(payload);
        state.name = payload.name
        state.token = payload.token
        localStorage.setItem('username', payload.name)
    }
}
const actions = {
    checkLogin({ commit }, payload) {
        return new Promise((resolve, reject) => {
            axios.post('login', payload)
                .then(
                    //  res => { console.log(res)} 
                    res => {
                        console.log(res);
                        if (res.data.meta.status == 200) { 
                            commit('getUsers', { name: res.data.data.username, token: res.data.data.token })
                            resolve(res.data)
                        }else{
                            reject(res.data)
                        }
                    }
                )
        })
    }

}

export default {
    state,
    mutations,
    actions,
    getters
}
